<?php

namespace App\Repository\Repositories;

use App\Repository\Models\ProductCategories;
use App\Repository\Models\Products;
use App\Repository\Models\ProductSkus;
use App\Repository\Repositories\Interfaces\ProductCategoriesRepository;

/**
 * Class ProductCategoriesRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class ProductCategoriesRepositoryEloquent extends BaseRepository implements ProductCategoriesRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
        'name'=> 'like',
        'show_status',
        'pid',
        'id'=> 'in',
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return ProductCategories::class;
    }

    //分类列表读取(分类模式基本定型所以就用简单嵌套循环读取数据，如以后有分类模式大规模更改可以改为无限循环模式读取数据)
    public function categoriesList(){
        //一级分类
        $data = $this->query()->where(['pid'=>0 , 'show_status'=>1,'level_type'=>'first_level'])->select('id','pid','name')->orderBy('sort')->get()->toArray();

        //通过一级分类查询二级分类
        foreach ($data as $key => $value){
            $two = $this->query()->where(['pid'=>$value['id'] , 'show_status'=>1,'level_type'=>'second_level'])->select('id','pid','name')->orderBy('sort')->get()->toArray();
            $data[$key]['second_list'] = $two;

            //通过二级分类查询三级分类
            foreach ($two as $key_t => $value_t){
                $three = $this->query()->where(['pid'=>$value_t['id'] , 'show_status'=>1,'level_type'=>'third_level'])->select('id','pid','name')->orderBy('sort')->get()->toArray();
                $data[$key]['second_list'][$key_t]['three_list'] = $three;

            }
        }

        return $data;
    }




    //首页推荐分类列表读取
    public function recommendCategoriesList(){
        //一级分类
        $data = $this->query()->where(['pid'=>0 , 'show_status'=>1,'level_type'=>'first_level','is_index_recommend'=>1])->select('id','pid','name')->orderBy('sort')->limit(6)->get()->toArray();

        //通过一级分类查询二级分类
        foreach ($data as $key => $value){
            $two = $this->query()->where(['pid'=>$value['id'] , 'show_status'=>1,'level_type'=>'second_level','is_index_recommend'=>1])->select('id','pid','name')->orderBy('sort','desc')->limit(5)->get()->toArray();

            $products = Products::query()->where(['first_cat'=>$value['id'],'is_for_sale'=>1,'is_index_recommend'=>1,'status'=>1,'product_type'=>1])->select('id','cover_image','name')->orderBy('sort','desc')->with(['product_skus'=>function($sql){
                $sql->select('id','product_id','price','stock','sale_price');
            }])->orderBy('index_sort','desc')->limit(3)->get()->toArray();


            $data[$key]['second_list'] = $two;//插入二级分类信息
            $data[$key]['products'] = $products;//插入产品
        }

        return $data;
    }

    /**
     * @TODO 首页推荐分类设置
     * */
    public function setRecommend($id,$status){
        try {
            $update = $this->query()->where('id', $id)->update(['is_index_recommend' => $status]);
        }catch (\Exception $e){
            return false;
        }
        return true;

    }
    
    public function setShowstatus($id,$status){
        try {
            $update = $this->query()->where('id', $id)->update(['show_status' => $status]);
        }catch (\Exception $e){
            return false;
        }
        return true;
        
    }

    public function recommendcate(){
        $recommendcate=$this->query()->where(['is_shopindex_recommend'=>1])->get(['id','pid','name','first_id','pic'])->toArray();

        $data=$this->list_to_tree($recommendcate);
        foreach($data as $k=>$v){
            $un_recommend=$this->query()->where(['is_shopindex_recommend'=>0,'pid'=>$v['id']])->paginate(6,['id','name','first_id','pic'])->toArray();
            $data[$k]['un_recommend']=$un_recommend['data'];
            $n=0;
            if(isset($v['child'])){
                foreach($v['child'] as $key=>$val){
                    if($n>3){
                        unset($data[$k]['child'][$key]);
                    }
                    $n++;
                }
            }

        }
        return $data;
    }

    /**
     * 商城首页-安防市场
     * @param array $list
     * @param string $pk
     * @param string $pid
     * @param string $child
     * @param int $root
     * @return array
     */
    public function list_to_tree(array $list, $pk = 'id', $pid = 'pid', $child = 'child', $root = 0)
    {
        // 创建Tree
        $tree = [];
        if(is_array($list)){
            // 创建基于主键的数组引用
            $refer = [];
            foreach ($list as $key => $data) {
                $refer[$data[$pk]] =& $list[$key];
            }
            $n=0;
            foreach ($list as $key => $data) {
                // 判断是否存在parent
                $parentId = $data[ $pid ];
                if($root == $parentId){
                    if($n<6){
                        $tree[] =& $list[ $key ];
                    }
                    $n++;
                } else {
                    if(isset($refer[ $parentId ])){
                        $parent =& $refer[ $parentId ];
                        $parent[ $child ][] =& $list[ $key ];
                    }
                }
            }
        }

        return $tree;
    }

    /**
     * @TODO 首页推荐分类设置
     * */
    public function setShopIndexRecommend($id,$status){
        try {
            $update = $this->query()->where('id', $id)->update(['is_shopindex_recommend' => $status]);
        }catch (\Exception $e){
            return false;
        }
        return true;

    }



    //商城首页安防市场推荐分类列表读取
    public function recommendShopIndexCategories(){
        //一级分类
        $data = $this->query()->where(['pid'=>0 , 'show_status'=>1,'level_type'=>'first_level','is_shopindex_recommend'=>1])->select('id','pid','name','pic')->orderBy('sort')->limit(6)->get()->toArray();

        //通过一级分类查询二级分类
        foreach ($data as $key => $value){
            $two = $this->query()->where(['pid'=>$value['id'] , 'show_status'=>1,'level_type'=>'second_level','is_shopindex_recommend'=>1])->select('id','pid','name')->orderBy('sort')->limit(6)->get()->toArray();

            $products = Products::query()->where(['first_cat'=>$value['id'],'is_for_sale'=>1,'is_index_recommend'=>1,'status'=>1,'product_type'=>1])->select('id','cover_image','name')->orderBy('sort','desc')->with(['product_skus'=>function($sql){
                $sql->select('id','product_id','price','stock','sale_price');
            }])->limit(4)->orderBy('index_sort','desc')->get()->toArray();

            $data[$key]['second_list'] = $two;//插入二级分类信息
            $data[$key]['products'] = $products;//插入产品
        }

        return $data;
    }


}
